package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Account;
import com.pojo.RightClassOne;
import com.pojo.Role;
import com.service.LoginService;
import com.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/roleController")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private LoginService loginService;

    @RequestMapping("/roleList")
    @ResponseBody
    public PageInfo<Role> showList(HttpSession session,Integer page) {
        /**
         * 根据用户属性判断数据权限——查询session父级id是什么：bf
         * 0则不传，1则传roleId，其他直接返回null
         * 1.超级管理员，不需要传参
         * 2.主商户及子商户，查看自己子级角色，参数
         */
        if(page==null || page==0){
            page=1;
        }
        PageHelper.startPage(page,9);
        Account thisAccount = (Account) session.getAttribute("thisAccount");
        Account account = new Account(thisAccount.getUserCode(),thisAccount.getUserName(),thisAccount.getPassWord(),thisAccount.getRealName(),thisAccount.getPhone(),thisAccount.getEmail(),thisAccount.getCreateDay(),thisAccount.getStatus(),thisAccount.getLoginTime(),thisAccount.getaId(),thisAccount.getAcountId(),thisAccount.getRoleId(),thisAccount.getRoleName(),thisAccount.getDescribe(),thisAccount.getSuperId(),thisAccount.getCreateDate(),thisAccount.getHead());
        PageInfo<Role> roleList = new PageInfo<Role>(roleService.showList(account),3);
//        System.out.println("roleList的值："+roleList);
        return roleList;
    }

    @RequestMapping("/findById")
    @ResponseBody
    public Role findById(String id) {
        return roleService.showRoleMsg(Long.valueOf(id));
    }

    @RequestMapping("/findRightDoById")
    @ResponseBody
    public List<RightClassOne> findRightDoById(Long id) {
        return loginService.thisAccountRightDoByRoleId(id);
    }


    /**
     * 根据id删除角色
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteRoleById")
    @ResponseBody
    public String deleteRoleById(Integer id) {
        if (roleService.deleteRoleById(id)) {
            return "1";
        }
        return "0";
    }

    /**
     * 模糊查询
     *
     * @param role
     * @return
     */
    @RequestMapping("/searchRoleByNameOrStatus")
    @ResponseBody
    public PageInfo<Role> searchRoleByNameOrStatus(Role role,HttpSession session,Integer page) {
        if(page==null || page==0){
            page=1;
        }
        PageHelper.startPage(page,9);
        Account thisAccount = (Account) session.getAttribute("thisAccount");
        Account account = new Account(thisAccount.getUserCode(),thisAccount.getUserName(),thisAccount.getPassWord(),thisAccount.getRealName(),thisAccount.getPhone(),thisAccount.getEmail(),thisAccount.getCreateDay(),thisAccount.getStatus(),thisAccount.getLoginTime(),thisAccount.getaId(),thisAccount.getAcountId(),thisAccount.getRoleId(),thisAccount.getRoleName(),thisAccount.getDescribe(),thisAccount.getSuperId(),thisAccount.getCreateDate(),thisAccount.getHead());
        role.setRoleId(account.getRoleId());
        PageInfo<Role> roleList = new PageInfo<Role>(roleService.searchRoleByNameOrStatus(role),3);
        return roleList;
    }

    /**
     * 添加主商户
     *
     * @param role
     * @return
     */
//    @RequestMapping("/addBusiness")
//    @ResponseBody
//    public String addBusiness(Role role) {
//        System.out.println(role);
//        if (roleService.addBusiness(role)) {
//            return "1";
//        }
//        return "0";
//    }

    /**
     * 1.修改角色信息
     *
     * @param role
     * @return
     */
    @RequestMapping("/updateBusiness")
    @ResponseBody
    public String updateBusiness(Role role,HttpSession session) {
        if (roleService.updateBusiness(role)) {
            session.setAttribute("updateRoleId",role.getRoleId());
            //若为商户，则所有子商户的子角色也都被禁用
            if(role.getSuperId()==null){
                if(roleService.updateBusinessChildsStatus(role)){
                    return "1";
                }
            }
            return "1";   //子商户，只修改当前状态
        }
        return "0";
    }
    /**
     * 2.修改角色权限
     * @param rights
     * @return
     */
    @RequestMapping("/updateBusinessRights")
    @ResponseBody
    public String testGetNodes(String rights, HttpSession session){
        Long updateRoleId = (Long) session.getAttribute("updateRoleId");   //修改的角色id
        System.out.println("需要修改权限的商户id:"+updateRoleId);
        rights = rights.substring(1,rights.length()-1); //去掉数组的[]
        /**
         * 1.清除对应角色的权限
         * 2.新增角色的权限
         */
        roleService.deleteRoleRights(updateRoleId);  //清除权限
        if (roleService.addRoleRights(rights,Long.valueOf(updateRoleId))) {
            return "1";
        }
        return "0";
    }

    /**
     * 展示所有一级商户
     *
     * @return
     */
    @RequestMapping("/superBusinessList")
    @ResponseBody
    public List<Role> superBusinessList() {
        return roleService.superBusinessList();
    }

    /**
     * 查询是否有操作权限
     *
     * @param session
     * @param roleId
     * @return
     */
    @RequestMapping("/checkRights")
    @ResponseBody
    public String checkRights(HttpSession session, Integer roleId) {
        List<RightClassOne> rightDoList = (List<RightClassOne>) session.getAttribute("RightDoList");
        for (RightClassOne r : rightDoList) {
            if (roleId == r.getRightId()) {
                return String.valueOf(r.getRightId());
            }
        }
        return "null";
    }


     /*树形结构*/
    @RequestMapping("/bandChildRole")
    @ResponseBody
    public List<RightClassOne> bandChildRightById(Integer id) {
        if (id == null) {
            id = 1;
        }
        return roleService.findRightsBySuperId(new ArrayList<RightClassOne>(),id);
    }
    @RequestMapping("/findAllRights")
    @ResponseBody
    public List<RightClassOne> findAllRights() {
        return roleService.findAllRights();
    }

    /**
     * 当前角色的一级菜单权限 ——pass方法
     * @return
     */
    @RequestMapping("/findThisAccountAllOneRights")
    @ResponseBody
    public List<RightClassOne> findThisAccountAllOneRights(HttpSession session) {
        List<RightClassOne> riOneList = (List<RightClassOne>) session.getAttribute("RiOneList");
        return riOneList;
    }
    /**
     * findAccountRights：查询当前角色的所有菜单权限_树状批量选中
     */
    @RequestMapping("/findAccountRights")
    @ResponseBody
    public List<RightClassOne> findAccountRights(Long id){
        return roleService.findAccountRights(id);
    }

}